﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace CourseSelectionSystem
{
    public class Database
    {
        
        public SqlConnection connection()
        {
            string str = "Data Source=LAPTOP-HK6EOMOS;Initial Catalog=CourseSelectionSystem2024;Persist Security Info=True;User ID=sa;Password=TNT021212123066;TrustServerCertificate=True";
            SqlConnection sc = new SqlConnection(str);
            //打开数据库链接
            sc.Open();
            return sc;
        }

        public SqlCommand command(string sql)
        {
            SqlCommand sc = new SqlCommand(sql,connection());
            return sc;
        }

        /// <summary>
        /// 用于delete update insert，返回受影响的行数
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public int Excute(string sql) 
        {
            return command(sql).ExecuteNonQuery();
        }

        /// <summary>
        /// 用于select，返回SqlDataReader对象，包含select到的数据
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public SqlDataReader read(string sql)
        {
            return command(sql).ExecuteReader();
        }
    }
}
